<?php
declare(strict_types=1);

namespace App\Logic\User\Repository;

use App\Exceptions\DbException;
use App\Models\User\ImageCollection;
use App\Models\User\ImageModel;
use Closure;

class ImageCollectionRepository implements UserRepositoryInterface
{

    /**
     * 用户收藏列表
     * @param Closure $closure
     * @param int $perSize
     * @param array $searchFields
     * @return array
     */
    public function repositorySelect(Closure $closure, int $perSize, array $searchFields = []): array
    {
        $items = ImageCollection::query()
            ->with(["image:uid,device_type,title,url,path"])
            ->where($closure)
            ->orderByDesc("id")
            ->paginate($perSize, $searchFields);

        return [
            "items" => $items->items(),
            "page"  => $items->currentPage(),
            "size"  => $perSize,
            "total" => $items->total(),
        ];
    }

    public function repositoryCreate(array $insertInfo): bool
    {
        try {
            $authorInfo = ImageModel::query()->where("uid", "=", $insertInfo["image_uid"])->first(["author_uid"]);
            if (empty($authorInfo)) {
                return false;
            }
            $authorInfo = $authorInfo->toArray();
            $insertInfo["author_uid"] = $authorInfo["author_uid"];
            $model = ImageCollection::query()->create($insertInfo);
            return !empty($model->getKey());
        } catch (\Throwable $throwable) {
            preg_match("/idx_ui/", $throwable->getMessage(), $str);
            if (!empty($str)) {
                throw new DbException("您已收藏");
            }
            return false;
        }
    }

    /**
     * 查询作者数据中心的收藏
     * @param Closure $closure
     * @param int $perSize
     * @param array $searchFields
     * @return array
     */
    public function repositoryAuthorSelect(Closure $closure, int $perSize, array $searchFields = []): array
    {
        $items = ImageCollection::query()
            ->with(["user:nickname,avatar_url,uid"])
            ->with(["image:uid,device_type,title"])
            ->where($closure)
            ->orderByDesc("id")
            ->paginate($perSize, $searchFields);

        return [
            "items" => $items->items(),
            "page"  => $items->currentPage(),
            "size"  => $perSize,
            "total" => $items->total(),
        ];
    }

    public function repositoryFind(Closure $closure, array $searchFields = []): array
    {
        $bean = ImageCollection::query()->where($closure)->first($searchFields);
        return !empty($bean) ? $bean->toArray() : [];
    }

    public function repositoryUpdate(array $updateWhere, array $updateInfo): int
    {
        return 0;
    }

    public function repositoryWhereInDelete(array $deleteWhere, string $field): int
    {
        return 0;
    }

    public function repositoryDelete(array $deleteWhere): int
    {
        return 0;
    }

    /**
     * 用户关注汇总
     * @param array $searchWhere
     * @return int
     */
    public function repositoryUserTotal(array $searchWhere): int
    {
        return ImageCollection::query()->where($searchWhere)->count();
    }
}
